{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [0.051073458, 0.099162638]\n",
      "20 [0.10137105, 0.19929245]\n",
      "40 [0.10076727, 0.19960411]\n",
      "60 [0.10042937, 0.19977847]\n",
      "80 [0.10024028, 0.19987603]\n",
      "100 [0.10013447, 0.19993061]\n",
      "120 [0.10007526, 0.19996117]\n",
      "140 [0.1000421, 0.19997828]\n",
      "160 [0.10002356, 0.19998784]\n",
      "180 [0.10001317, 0.19999319]\n",
      "200 [0.10000738, 0.19999619]\n"
     ]
    }
   ],
   "source": [
    "#sample\n",
    "x_data = np.random.rand(100)\n",
    "y_data = x_data*0.1 + 0.2\n",
    "\n",
    "#liner model\n",
    "b = tf.Variable(0.)\n",
    "k = tf.Variable(0.)\n",
    "y = k*x_data + b\n",
    "\n",
    "#二次代价函数 先误差的平方，再求一个平均值\n",
    "loss = tf.reduce_mean(tf.square(y_data-y))\n",
    "#定义-个梯度下降法来进行训练的优化器\n",
    "optimizer = tf.train.GradientDescentOptimizer(0.2)\n",
    "#最小化代价函数\n",
    "train = optimizer.minimize(loss)\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for step in range(201):\n",
    "        sess.run(train)\n",
    "        if step%20==0:\n",
    "            print(step,sess.run([k,b]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
